/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.common;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.SystemProperties;
import android.view.Display;
import android.view.View;

import com.ecarx.systemui.plugin.psd.PsdBarsManager;
import com.ecarx.systemui.plugin.utils.LogUtils;



/**
 * Various utilities shared amongst the Launcher's classes.
 */
public final class CommonUtils {
    public static final boolean ATLEAST_LOLLIPOP_MR1 =
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1;
    public static final boolean ATLEAST_LOLLIPOP =
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;

    public static final int CPAA_OPEN = 1;

    public static final String TAG = "Utilities-Plugin";

    // 两次点击间隔不能少于250ms
    private static final int FAST_CLICK_DELAY_TIME = 250;
    private static long lastClickTime;

    /**
     * Utility method to determine whether the given point, in local coordinates,
     * is inside the view, where the area of the view is expanded by the slop factor.
     * This method is called while processing touch-move events to determine if the event
     * is still within the view.
     */
    public static boolean pointInView(View v, float localX, float localY, float slop) {
        return localX >= -slop && localY >= -slop && localX < (v.getWidth() + slop) &&
                localY < (v.getHeight() + slop);
    }

    public static boolean isEU() {
        String a = SystemProperties.get("ro.product.device");
        LogUtils.d(TAG, "isEUDevice: " + a);
        return "smart_eu".equals(a);
    }

    private static int getCurrentCPAA() {
        int currentCA = SystemProperties.getInt("persist.current.cpaa",0);
        LogUtils.d(TAG,"currentCA:"+currentCA);
        return currentCA;
    }

    public static boolean isNeedCPAA(){
        return getCurrentCPAA() == 1 && CommonUtils.isEU();
    }

    public static boolean isCP_OK() {
        int cp = SystemProperties.getInt("persist.cp.enable", 0);
        LogUtils.d(TAG, "isCP_OK: " + cp);
        return cp == CPAA_OPEN;
    }

    public static boolean isAA_OK() {
        int aa = SystemProperties.getInt("persist.aa.enable", 0);
        LogUtils.d(TAG, "isAA_OK: " + aa);
        return aa == CPAA_OPEN;
    }

    public static void enableLaunchComponent(Context context, ComponentName componentName, boolean enable) {
        if (context == null) {
            return;
        }
        PackageManager pm = context.getPackageManager();
        try {
            if (componentName == null) {
                return;
            }
            int state = pm.getComponentEnabledSetting(componentName);
            LogUtils.w(TAG, "[JERRY][enablePackage]"
                    + ", ComponentName: " + componentName
                    + ", state: " + state
                    + ", enable: " + enable
            );
            if (enable) {
                pm.setComponentEnabledSetting(componentName,
                        PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
                        PackageManager.DONT_KILL_APP);
            } else {
                pm.setComponentEnabledSetting(componentName,
                        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                        0);
            }
        } catch (Exception e) {
            LogUtils.e(TAG, "[JERRY][enablePackage]"
                    + ", Exception: " + e.getMessage()
            );
        }
    }

    public static Context getDisplayContext(Context context, boolean isCsd) {
        DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
        Context displayContext = isCsd ? context.createDisplayContext(display) :
                PsdBarsManager.getInstance().getPSDContext();
        return displayContext;
    }




    public static boolean isFastClick() {
        boolean flag = true;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= FAST_CLICK_DELAY_TIME ) {
            flag = false;
        }
        lastClickTime = currentClickTime;
        return flag;
    }


    public static int getDisplayId(Context context, boolean isCSD) {
        int id = 0;
        DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = displayManager.getDisplays();
        for (Display display : displays) {
            if (isCSD) {
                if (display.getDisplayId() == Display.DEFAULT_DISPLAY) { // CSD
                    return display.getDisplayId();
                }
            } else {
                if (display.getDisplayId() != Display.DEFAULT_DISPLAY && display.getType() == Display.TYPE_INTERNAL) { // PSD
                    return display.getDisplayId();
                }
            }
        }
        return id;
    }



}
